<?php

require __DIR__ . '/../../vendor/autoload.php';

use Parse\RecursiveParser;
use Parse\CombinatorParser;
use Parse\StringParser;
use Parse\CondParser;
use Parse\RegexParser;
use Parse\ParseException;

$parser = new RecursiveParser(
    "paren_expr",
    array(
        "paren_expr" => new CombinatorParser(
            array("open_paren",
                  "expr",
                  "close_paren")
        ),
        "open_paren" => new StringParser("("),
        "expr" => new CondParser(
            array("paren_expr", "number")
        ),
        "number" => new  RegexParser(
            "#^-?(0|[1-9][0-9]*)(\.[0-9]+)?([eE][-+]?[0-9]+)?#",
            function ($match) {
                    return (float)$match;
            }
        ),
        "close_paren" => new StringParser(")"))
);

var_dump($parser->parse("((1))") ===
         array("(", array("(", (float)1, ")"), ")"));

try {
    var_dump($parser->parse("((10)"));
    var_dump(false);
} catch (ParseException $e) {
    var_dump(true);
}
               
try {
    var_dump($parser->parse("((hi mom!))"));
    var_dump(false);
} catch (ParseException $e) {
    var_dump(true);
}
